<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>对象相关的运算符和内置函数 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter06.html" />
    
    
    <link rel="prev" href="./chapter04.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="5"
        data-chapter-title="对象相关的运算符和内置函数"
        data-filepath="chapter05.md"
        data-basepath="."
        data-revision="Sun Feb 23 2020 13:59:40 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        类方法和实例方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        单例设计模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        继承的基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        继承的注意使用
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        对象相关的运算符和内置函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        多态的使用
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x5BF9;&#x8C61;&#x76F8;&#x5173;&#x7684;&#x5185;&#x7F6E;&#x51FD;&#x6570;">&#x5BF9;&#x8C61;&#x76F8;&#x5173;&#x7684;&#x5185;&#x7F6E;&#x51FD;&#x6570;</h2>
<p>Python&#x4E2D;&#x7684;&#x8EAB;&#x4EFD;&#x8FD0;&#x7B97;&#x7B26;&#x7528;&#x6765;&#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x76F8;&#x7B49;&#xFF1B;isinstance&#x7528;&#x6765;&#x5224;&#x65AD;&#x5BF9;&#x8C61;&#x548C;&#x7C7B;&#x4E4B;&#x95F4;&#x7684;&#x5173;&#x7CFB;&#xFF1B;issublcass&#x7528;&#x554A;&#x91CC;&#x5224;&#x65AD;&#x7C7B;&#x4E0E;&#x7C7B;&#x4E4B;&#x95F4;&#x7684;&#x5173;&#x7CFB;&#x3002;</p>
<h3 id="&#x8EAB;&#x4EFD;&#x8FD0;&#x7B97;&#x7B26;">&#x8EAB;&#x4EFD;&#x8FD0;&#x7B97;&#x7B26;</h3>
<p>&#x8EAB;&#x4EFD;&#x8FD0;&#x7B97;&#x7B26;&#x7528;&#x6765;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5185;&#x5B58;&#x5730;&#x5740;&#xFF0C;&#x770B;&#x8FD9;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, age)</span>:</span>
        self.name = name
        self.age = age


p1 = Person(<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>, <span class="hljs-number">18</span>)
p2 = Person(<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>, <span class="hljs-number">18</span>)
p3 = p1

print(p1 <span class="hljs-keyword">is</span> p2)  <span class="hljs-comment"># False</span>
print(p1 <span class="hljs-keyword">is</span> p3)  <span class="hljs-comment"># True</span>
</code></pre>
<h3 id="isinstance">isinstance</h3>
<p>instance&#x5185;&#x7F6E;&#x51FD;&#x6570;&#xFF0C;&#x7528;&#x6765;&#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x662F;&#x7531;&#x67D0;&#x4E00;&#x4E2A;&#x7C7B;(&#x6216;&#x8005;&#x5B83;&#x7684;&#x5B50;&#x7C7B;)&#x5B9E;&#x4F8B;&#x5316;&#x521B;&#x5EFA;&#x51FA;&#x6765;&#x7684;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, age)</span>:</span>
        self.name = name
        self.age = age


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span><span class="hljs-params">(Person)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, age, score)</span>:</span>
        super(Student, self).__init__(name, age)
        self.score = score


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, color)</span>:</span>
        self.name = name
        self.color = color


p = Person(<span class="hljs-string">&apos;tony&apos;</span>, <span class="hljs-number">18</span>)
s = Student(<span class="hljs-string">&apos;jack&apos;</span>, <span class="hljs-number">20</span>, <span class="hljs-number">90</span>)
d = Dog(<span class="hljs-string">&apos;&#x65FA;&#x8D22;&apos;</span>, <span class="hljs-string">&apos;&#x767D;&#x8272;&apos;</span>)

print(isinstance(p, Person))  <span class="hljs-comment"># True.&#x5BF9;&#x8C61;p&#x662F;&#x7531;Person&#x7C7B;&#x521B;&#x5EFA;&#x51FA;&#x6765;&#x7684;</span>
print(isinstance(s, Person))  <span class="hljs-comment"># True.&#x5BF9;&#x8C61;s&#x662F;&#x6709;Person&#x7C7B;&#x7684;&#x5B50;&#x7C7B;&#x521B;&#x5EFA;&#x51FA;&#x6765;&#x7684;</span>
print(isinstance(d, Person))  <span class="hljs-comment"># False.&#x5BF9;&#x8C61;d&#x548C;Person&#x7C7B;&#x6CA1;&#x6709;&#x5173;&#x7CFB;</span>
</code></pre>
<h3 id="issubclass">issubclass</h3>
<p>issubclass &#x7528;&#x6765;&#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x7C7B;&#x4E4B;&#x95F4;&#x7684;&#x7EE7;&#x627F;&#x5173;&#x7CFB;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, age)</span>:</span>
        self.name = name
        self.age = age


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span><span class="hljs-params">(Person)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, age, score)</span>:</span>
        super(Student, self).__init__(name, age)
        self.score = score


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, color)</span>:</span>
        self.name = name
        self.color = color


print(issubclass(Student, Person))  <span class="hljs-comment"># True</span>
print(issubclass(Dog, Person))  <span class="hljs-comment"># False</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter04.html" class="navigation navigation-prev " aria-label="Previous page: 继承的注意使用"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter06.html" class="navigation navigation-next " aria-label="Next page: 多态的使用"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
